home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / udp.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  2KB  |  91 lines

  1. /*
  2.     udp test translated into rexx
  3.     from Amiga Magazine n.93 udp.c test by Rudi Chiarito
  4.  
  5.     Show the use of udp protocol.
  6.     Usage: udp <host> <port> <localPort>
  7.  
  8.     i.e.:
  9.         shell1 udp localhost 4000 4001
  10.         shell2 udp localhost 4001 4000
  11.  
  12.     to stop push <ctrl-c> in the shells.
  13.  
  14.     To see how rx/tx is stopped just write something in one or both
  15.     the shells.
  16.  
  17. */
  18.  
  19. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  20. if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then exit
  21.  
  22. parse arg host remotePort localPort .
  23.  
  24. if IsDotAddr(host) then do
  25.     remote.addrFamily = "INET"
  26.     remote.addrAddr   = host
  27. end
  28. else do
  29.     if ~gethostbyname("H",host) then do
  30.         say "udp: host" host "not found."
  31.         exit
  32.     end
  33.     remote.addrFamily = h.hostAddrType
  34.     remote.addrAddr   = h.hostAddrList.0
  35. end
  36.  
  37. remote.addrPort = remotePort
  38.  
  39. sock = socket("INET","DGRAM","IP")
  40. if sock<0 then do
  41.     say "udp: can't create socket:" errno()
  42.     exit
  43. end
  44.  
  45. local.addrFamily = "INET"
  46. local.addrAddr     = 0
  47. local.addrPort   = localPort
  48. if bind(sock,"LOCAL")<0 then do
  49.     say "udp: can't bind port:" errno()
  50.     exit
  51. end
  52.  
  53. call IOCtlSocket(sock,"FIONBIO",1)
  54.  
  55. data = "Ciao"
  56. dataLen = length(data)
  57.  
  58. down = 0
  59. do while 1
  60.  
  61.     n = SendTo(sock,data,0,"REMOTE")
  62.     if n!=dataLen then do
  63.         say "udp SendTo() error:" errno()
  64.         exit
  65.     end
  66.  
  67.     call delay(10)
  68.  
  69.     if down then call WriteCh("STDOUT",".")
  70.  
  71.     n = RecvFrom(sock,"BUFF",10,0,"REMOTE")
  72.     if n==-1 then do
  73.         err = errno()
  74.         if err==35 then do
  75.             if ~down then do
  76.                 call WriteCh("STDOUT","udp: no more data.")
  77.                 down = 1
  78.             end
  79.         end
  80.         else do
  81.             say "udp: RecvFrom() error:" err
  82.             exit
  83.         end
  84.     end
  85.     else do
  86.         say "udp: read byte(s):" n
  87.         down = 0
  88.     end
  89.  
  90. end
  91.